package com.vf.cloud.server.colony.channel;

import java.util.concurrent.TimeUnit;

import com.vf.cloud.server.colony.handler.NodeChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateHandler;

public class NodeChannelInitializer extends ChannelInitializer<SocketChannel> {

	@Override
	protected void initChannel(SocketChannel ch) throws Exception {
		ch.pipeline()
				// 设置30秒没有读到数据，则触发一个READER_IDLE事件。
				.addLast(new IdleStateHandler(0, 30, 0, TimeUnit.SECONDS)).addLast(new HttpServerCodec())
				.addLast(new StringEncoder())// 添加字符串编码器
				.addLast(new HttpObjectAggregator(65536)).addLast(new ChunkedWriteHandler())
				.addLast(new StringEncoder())
				.addLast(new DelimiterBasedFrameDecoder(Integer.MAX_VALUE, Delimiters.lineDelimiter()[0]))
				.addLast(new NodeChannelInboundHandlerAdapter());
	}

}
